package com.czwj.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.dao.AttachmentMapper;
import com.czwj.dao.AttachmentRelyMapper;
import com.czwj.dao.EquipmentMapper;
import com.czwj.dao.EquipmentMarkMapper;
import com.czwj.entities.Attachment;
import com.czwj.entities.AttachmentRely;
import com.czwj.entities.Equipment;
import com.czwj.entities.EquipmentMark;
import com.czwj.entities.FinishApply;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.ProjectItemChild;
import com.czwj.entities.UserInfo;
import com.czwj.service.EquipmentMarkService;
import com.czwj.util.CheckFileFormatUtil;
import com.czwj.util.TextUtil;

/**
* @ClassName: EquipmentMarkServiceImpl
* @Description: TODO
* @author H
* @date 2020年4月9日
*
*/
@Service
public class EquipmentMarkServiceImpl implements EquipmentMarkService{
	@Resource
	EquipmentMarkMapper mapper;
	
	@Resource
	EquipmentMapper itemmapper;

	@Resource
	AttachmentMapper amapper;
	
	@Resource
	AttachmentRelyMapper relymapper;
	
	@Value("${resources.phypath}")
	private String PhyPath;
	
	@Value("${resources.respath}")
	private String ResPath;
	
	private final String Attachment_Finish_Apply_Path = "设备维保/";
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.EquipmentMarkService#search(java.util.Map)
	*/
	@Override
	public List<EquipmentMark> search(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.search(map);
	}
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.EquipmentMarkService#count(java.util.Map)
	*/
	@Override
	public int count(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.count(map);
	}
	
	/** 
	* 
	* @param equipmentMark
	* @return
	* @see com.czwj.service.EquipmentMarkService#update(com.czwj.entities.EquipmentMark)
	*/
	@Override
	public int update(EquipmentMark equipmentMark){
		// TODO Auto-generated method stub
		return this.mapper.updateByPrimaryKeySelective(equipmentMark);
	}
	
	/** 
	* 
	* @param equipmentMark
	* @return
	* @see com.czwj.service.EquipmentMarkService#insert(com.czwj.entities.EquipmentMark)
	*/
	@Override
	public int insert(EquipmentMark equipmentMark){
		// TODO Auto-generated method stub
		return this.mapper.insertSelective(equipmentMark);
	}
	
	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.EquipmentMarkService#delete(int)
	*/
	@Override
	public int delete(int id){
		// TODO Auto-generated method stub
		return this.mapper.deleteByPrimaryKey(id);
	}
	
	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.EquipmentMarkService#selectByPrimaryKey(java.lang.Integer)
	*/
	@Override
	public EquipmentMark selectByPrimaryKey(Integer id){
		// TODO Auto-generated method stub
		return this.mapper.selectByPrimaryKey(id);
	}
	
	/** 
	* 
	* @return
	* @see com.czwj.service.EquipmentMarkService#searchWarning()
	*/
	@Override
	public List<EquipmentMark> searchWarning(){
		// TODO Auto-generated method stub
		return this.mapper.searchWarning();
	}
	
	/** 
	* 
	* @return
	* @see com.czwj.service.EquipmentMarkService#searchWarning2()
	*/
	@Override
	public List<EquipmentMark> searchWarning2(){
		// TODO Auto-generated method stub
		return this.mapper.searchWarning2();
	}
	
	/** 
	* 
	* @param mark
	* @return
	* @see com.czwj.service.EquipmentMarkService#updateStatus(com.czwj.entities.EquipmentMark)
	*/
	@Override
	public int updateStatus(EquipmentMark mark){
		// TODO Auto-generated method stub
		return this.mapper.updateByPrimaryKeySelective(mark);
	}
	
	/** 
	* 
	* @param id
	* @param equipmentid
	* @param starttime
	* @param intervaltime
	* @param isdaily
	* @param remark
	* @param createtime
	* @param images
	* @return
	* @see com.czwj.service.EquipmentMarkService#insertorupdate(java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.lang.String, org.springframework.web.multipart.MultipartFile[])
	*/
	@Transactional
	@Override
	public int insertorupdate(Integer id,Integer equipmentid,String starttime,String intervaltime,Integer isdaily,
			String remark,String createtime,MultipartFile[] images,HttpServletRequest request){
		// TODO Auto-generated method stub
		int count = 0;
		Subject currentUser = SecurityUtils.getSubject();
		int createrid = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId();
		String createip = request.getRemoteAddr();
		Date time1;
		Date time2;
		if(id == null){
			try{
				if(TextUtil.isNotEmpty(starttime)){
					time1 = sdf.parse(starttime);
				}else{
					time1 = null;
				}
				if(TextUtil.isNotEmpty(createtime)){
					time2 = sdf.parse(createtime);
				}else{
					time2 = null;
				}
				EquipmentMark equipmentMark = new EquipmentMark(id,equipmentid,time1,intervaltime,isdaily,remark,time2,
						1);
				count += this.mapper.insertSelective(equipmentMark);
				Equipment item = this.itemmapper.selectByPrimaryKey(equipmentid);
				String path = item.getName() + "/" + Attachment_Finish_Apply_Path + sdf.format(new Date()) + "/";
				String sourcepath = PhyPath + path;// 存储路径
				String urlpath = ResPath + path;// 资源路径
				File dest = new File(sourcepath);
				if(!dest.exists()){
					dest.mkdirs();
				}
				for(MultipartFile _file:images){
					String filename = _file.getOriginalFilename();
					String _sourcepath = sourcepath + filename;
					String _urlpath = urlpath + filename;
					File file = new File(_sourcepath);
					while(file.exists()){
						String[] arr = _sourcepath.split("\\.");
						_sourcepath = "";
						for(int i = 0;i < arr.length;i++){
							if(i < arr.length - 1){
								_sourcepath += arr[i];;
							}else{
								_sourcepath += "(1)." + arr[i];
							}
						}
						file = new File(_sourcepath);
					}
					_file.transferTo(new File(_sourcepath));
					String suffix = filename.substring(filename.lastIndexOf(".") + 1);
					String type = "";
					if(suffix == null){
						type = "other";
					}else if(suffix.contains("doc")){
						type = "doc";
					}else if(suffix.contains("xls")){
						type = "xls";
					}else if(suffix.contains("ppt")){
						type = "ppt";
					}else{
						type = CheckFileFormatUtil.getFileType(_sourcepath);
					}
					Attachment attachment = new Attachment(filename,_urlpath,_sourcepath,type,createrid,new Date(),
							createip);
					count += this.amapper.insertSelective(attachment);
					AttachmentRely aRely = new AttachmentRely("equipmentmark",equipmentMark.getId(),attachment.getId());
					count += this.relymapper.insertSelective(aRely);
				}
			}
			catch(Exception e){
				// TODO Auto-generated catch block
				return 0;
			}
		}else{
			try{
				if(TextUtil.isNotEmpty(starttime)){
					time1 = sdf.parse(starttime);
				}else{
					time1 = null;
				}
				if(TextUtil.isNotEmpty(createtime)){
					time2 = sdf.parse(createtime);
				}else{
					time2 = null;
				}
				EquipmentMark equipmentMark = new EquipmentMark(id,equipmentid,time1,intervaltime,isdaily,remark,time2,
						1);
				count += this.mapper.updateByPrimaryKeySelective(equipmentMark);
				Equipment item = this.itemmapper.selectByPrimaryKey(equipmentid);
				String path = item.getName() + "/" + Attachment_Finish_Apply_Path + sdf.format(new Date()) + "/";
				String sourcepath = PhyPath + path;// 存储路径
				String urlpath = ResPath + path;// 资源路径
				File dest = new File(sourcepath);
				if(!dest.exists()){
					dest.mkdirs();
				}
				for(MultipartFile _file:images){
					String filename = _file.getOriginalFilename();
					String _sourcepath = sourcepath + filename;
					String _urlpath = urlpath + filename;
					File file = new File(_sourcepath);
					while(file.exists()){
						String[] arr = _sourcepath.split("\\.");
						_sourcepath = "";
						for(int i = 0;i < arr.length;i++){
							if(i < arr.length - 1){
								_sourcepath += arr[i];;
							}else{
								_sourcepath += "(1)." + arr[i];
							}
						}
						file = new File(_sourcepath);
					}
					_file.transferTo(new File(_sourcepath));
					String suffix = filename.substring(filename.lastIndexOf(".") + 1);
					String type = "";
					if(suffix == null){
						type = "other";
					}else if(suffix.contains("doc")){
						type = "doc";
					}else if(suffix.contains("xls")){
						type = "xls";
					}else if(suffix.contains("ppt")){
						type = "ppt";
					}else{
						type = CheckFileFormatUtil.getFileType(_sourcepath);
					}
					Attachment attachment = new Attachment(filename,_urlpath,_sourcepath,type,createrid,new Date(),
							createip);
					count += this.amapper.insertSelective(attachment);
					AttachmentRely aRely = new AttachmentRely("equipmentmark",equipmentMark.getId(),attachment.getId());
					count += this.relymapper.insertSelective(aRely);
				}
			}
			catch(Exception e){
				// TODO Auto-generated catch block
				return 0;
			}
		}
		return count;
	}
}
